package com.hdax.dm.service;

import com.hdax.dm.dto.CommonResponse;
import com.hdax.dm.dto.DtoUtil;
import com.hdax.dm.dto.scheduler.SchedulerPriceDto;
import com.hdax.dm.entity.item.DmCinemaSeat;
import com.hdax.dm.entity.scheduler.DmSchedulerSeatPrice;
import com.hdax.dm.feign.item.DmCinemaSeatFeign;
import com.hdax.dm.feign.scheduler.DmSchedulerFeign;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SeatListService {

    @Autowired
    private DmCinemaSeatFeign seatFeign;
    @Autowired
    private DmSchedulerFeign schedulerFeign;

    //剧场座位信息
    public List<String> getSeatList(Long schduleId){
        List<DmCinemaSeat> list = seatFeign.getSeatList(schduleId);
        List<String> seats = new ArrayList<>();
        //根据X轴分组
        Map<Long, List<DmCinemaSeat>> map = list.stream().collect(Collectors.groupingBy(DmCinemaSeat::getX));
        map.forEach((key,value)->{
            StringBuffer buffer = new StringBuffer();
            //循环对应X轴的Y轴信息
            value.forEach(seat->{
                //判断座位的具体信息
                if (seat.getStatus() == 0) {
                    buffer.append("_");
                }else if(seat.getStatus() == 1){
                    if(seat.getAreaLevel() == 1){
                        buffer.append("a");
                    }else if(seat.getAreaLevel() == 2){
                        buffer.append("b");
                    }else{
                        buffer.append("c");
                    }
                }
            });
            seats.add(buffer.toString());
        });
        System.out.println(seats);
        return seats;
    }

    //排期座位信息
    public Map<String,Object> getSchedule(Long schduleId){
        Map<String,Object> map = new HashMap<>();
        //封装价格
        List<Map<String,Object>> priceList = new ArrayList<>();
        List<DmSchedulerSeatPrice> priceDtos = schedulerFeign.getPrice(schduleId);
        priceDtos.forEach(price -> {
            Map<String,Object> mapPrice = new HashMap<>();
            if (price.getAreaLevel() == 1) {
                mapPrice.put("areaLevelName", "A");
            } else if (price.getAreaLevel() == 2) {
                mapPrice.put("areaLevelName", "B");
            } else {
                mapPrice.put("areaLevelName", "C");
            }
            mapPrice.put("price",price.getPrice());
            priceList.add(mapPrice);
        });
        map.put("seatPriceList",priceList);
        map.put("seatInfoList",schedulerFeign.getSeat(schduleId));
        return map;
    }
}
